/**
 * Copyright 2011, Under-Ground Studio.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.edu.zju.vlis.aus.util;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.widget.ImageView;
import cn.edu.zju.vlis.aus.R;
import cn.edu.zju.vlis.aus.bean.UsageRecord;
import cn.edu.zju.vlis.aus.db.UsageRecordDAO;

/**
 * This class is a static class to cache icon of applications.
 * 
 * @author ymsong (songyuming1985@gmail.com) 2011-9-25
 */
public class IconCache {

	private static Drawable defaultIcon;
	private static HashMap<String, Drawable> cache;

	static {
		cache = new HashMap<String, Drawable>();
	}

	public static Drawable getDefaultIcon() {
		return defaultIcon;
	}

	public static void loadDefaultIcon(Context context) {
		defaultIcon = context.getResources().getDrawable(R.drawable.stub);
	}

	/**
	 * Get icon for package in the cache.
	 * [!important: you should not modify the drawable]
	 * 
	 * @param packageName
	 * @return
	 */
	public static Drawable getIcon(String packageName) {
		return cache.get(packageName);
	}

	/**
	 * Put icon for package.
	 * 
	 * @param packageName
	 * @param icon
	 */
//	public static synchronized void setIcon(String packageName, Drawable icon) {
//		cache.put(packageName, icon);
//	}

	/**
	 * load all icon for packages.
	 * 
	 * @author ymsong (songyuming1985@gmail.com) 2011-9-25
	 */
	public static void loadAllIcons(Context context) {
		UsageRecordDAO dao = new UsageRecordDAO(context);
		List<UsageRecord> recList = dao.getAllStatistic();
		Drawable drawable = null;
		PackageManager pm = context.getPackageManager();
		for (UsageRecord rec : recList) {
			drawable = cache.get(rec.getPackageName());
			if (drawable == null) {
				try {
					PackageInfo pInfo = pm.getPackageInfo(rec.getPackageName(),
							0);
					drawable = pInfo.applicationInfo.loadIcon(pm);
				} catch (NameNotFoundException e) {
				}
				if (drawable != null) {
					cache.put(rec.getPackageName(), drawable);
				}
			}
		}
		dao.close();
	}

	/**
	 * A AsyncTask to load single icon for package.
	 * 
	 * @author ymsong (songyuming1985@gmail.com) 2011-9-25
	 */
	public static class LoadSingleIconTask extends
			AsyncTask<Void, Void, Drawable> {
		private Context context;
		private String packageName;
		private ImageView imageView;

		public LoadSingleIconTask(Context context, String packageName,
				ImageView view) {
			this.context = context;
			this.packageName = packageName;
			this.imageView = view;
		}

		@Override
		protected Drawable doInBackground(Void... voids) {
			Drawable drawable = cache.get(packageName);
			if (drawable != null) {
				return drawable;
			}
			PackageManager pm = context.getPackageManager();
			try {
				PackageInfo pInfo = pm.getPackageInfo(packageName, 0);
				drawable = pInfo.applicationInfo.loadIcon(pm);
			} catch (NameNotFoundException e) {
			}
			return drawable;
		}

		@Override
		protected void onPostExecute(Drawable result) {
			// if retrieve icon successfully
			if (result != null) {
				cache.put(packageName, result);
				imageView.setImageDrawable(result);
			}
		}
	}
}
